#include "hwboard.h"
#include "test_case.h"
#include "flashFs.h"
void test_spiffs()
{
    extern FlashFs flash_fs;
    UNITY_BEGIN(); // IMPORTANT LINE!

    TEST_ASSERT_EQUAL_UINT32(0xef4018, flash_fs.flash_chip_code());
    TEST_ASSERT_EQUAL_UINT32(1024 * 1024 * 16, flash_fs.flash_size());
    flash_fs.dir();
    FlashFile *ttt = new FlashFile();
    ttt->open_write("init.txt");
    ttt->write("1", 1);
    ttt->write("2", 1);
    ttt->write("3", 1);
    ttt->write("4", 1);
    ttt->close();
    FlashFile *rrr = new FlashFile();
    rrr->open_rdonly("init.txt");
    char buf[16];
    memset(buf, 0, sizeof(buf));
    rrr->read(buf, 16);
    rrr->close();
    TEST_ASSERT_EQUAL_STRING_MESSAGE("1234", buf, "TXT READ");
}
extern uint8_t di[], di_nrs;
void test_di_init()
{
    UNITY_BEGIN(); // IMPORTANT LINE!

    for (int i = 0; i < di_nrs; i++)
        TEST_ASSERT_EQUAL_STRING_MESSAGE(HIGH, digitalRead(di[i]), "DI");
}
extern const char *hwModel;

#ifdef LWIP
#include "ArduinoHttpClient.h"
#include "EthernetClient.h"
extern "C"{
void test_report(const char *msg)
{
    EthernetClient client;
    HttpClient http(client, "www.kaikong.cn", 8011);
    char url[256];
    unsigned int id[3];
    HAL_GetUID(id);
    snprintf(url, 256, "/hwtest?sn=%08x%08x%08x&msg=%s&uptime=%d", id[0], id[1], id[2], msg, uptime);
    core_debug("[HTTP] begin...\n");
    int httpCode = http.get(url);
    core_debug("[HTTP] GET... code: %d\n", httpCode);
    // http.stop();
}
void test_hw_reg()
{
    UNITY_BEGIN();
    EthernetClient client;
    HttpClient http(client, "www.kaikong.cn", 8011);
    char url[256];
    unsigned int id[3];
    HAL_GetUID(id);
    snprintf(url, 256, "/hwproduct?sn=%08x%08x%08x&model=%s&uptime=%d", id[0], id[1], id[2], hwModel, uptime);
    int httpCode = http.get(url);
    core_debug("hw/reg GET... code: %d\n", httpCode);
    http.stop();
    TEST_ASSERT_EQUAL_MESSAGE(0, httpCode, "net reg");
}
}
#else
#include <ELClientRest.h>
void test_hw_reg()
{
    extern ELClientRest rest;
    int err = rest.begin("www.kaikong.cn", 8011);
    if (!err)
    {
        char url[256];
        uint32_t id[3];
        HAL_GetUID(id);
        snprintf(url, 256, "/hwproduct?sn=%08x%08x%08x&model=%s&uptime=%d", id[0], id[1], id[2], hwModel, uptime);
        rest.get(url);
        char response[266];
        memset(response, 0, 266);
        uint16_t code = rest.waitResponse(response, 266);
        if (code == 200)
        {
            core_debug("ARDUINO: GET successful:%s", response);
        }
        else
        {
            core_debug("ARDUINO: GET failed: %d\n", code);
        }
    }
}
void test_report(const char *msg)
{
    extern ELClientRest rest;
    int err = rest.begin("www.kaikong.cn", 8011);
    if (!err)
    {
        char url[256];
        unsigned int id[3];
        HAL_GetUID((uint32_t *)id);
        snprintf(url, 256, "/hwtest?sn=%08x%08x%08x&msg=%s&uptime=%d", id[0], id[1], id[2], msg, uptime);
        rest.get(url);
        char response[266];
        memset(response, 0, 266);
        uint16_t code = rest.waitResponse(response, 266);
        if (code == 200)
        {
            core_debug("ARDUINO: GET successful:%s", response);
        }
        else
        {
            core_debug("ARDUINO: GET failed: %d\n", code);
        }
    }
}
#endif

#ifdef PCF8563
#include "Rtc_Pcf8563.h"
extern Rtc_Pcf8563 pcf8563;
void test_pcf8563()
{
    UNITY_BEGIN();
    pcf8563.setDateTime(18, 1, 8, 0, 19, 22, 30, 30);
    pcf8563.getDateTime();
    TEST_ASSERT_EQUAL_MESSAGE(18, pcf8563.getDay(), "rtc");
    TEST_ASSERT_EQUAL_MESSAGE(8, pcf8563.getMonth(), "rtc");
    TEST_ASSERT_EQUAL_MESSAGE(19, pcf8563.getYear(), "rtc");
    TEST_ASSERT_EQUAL_MESSAGE(22, pcf8563.getHour(), "rtc");
    TEST_ASSERT_EQUAL_MESSAGE(30, pcf8563.getMinute(), "rtc");
}

#endif
int test_run(int step)
{
    switch (step)
    {
    case 100:
        test_littlefs();
        break;
    case 102:
        test_bench();
        break;
    }
}